package com.sxran.im.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxran.im.bean.MessageReqBody;
import com.sxran.im.controller.ImController;
import com.sxran.im.dao.ImChatBodyMapper;
import com.sxran.im.dao.ImMsgExtrasMapper;
import com.sxran.im.dao.ImUserGroupMapper;
import com.sxran.im.info.ImChatBody;
import com.sxran.im.info.ImMsgExtras;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ChatService {
    @Resource
    private ImChatBodyMapper imChatBodyMapper;
    @Resource
    private ImMsgExtrasMapper imMsgExtrasMapper;

    public List<ImChatBody> selectList(MessageReqBody json) {
        //
        Integer type = json.getType(); //TODO 离线或者历史消息。

        Date endTime = json.getEndTime();
        Date beginTime = json.getBeginTime();
        boolean isTimeBetween = (beginTime != null && endTime != null);
        Integer offset = json.getOffset();
        Integer count = json.getCount();
        boolean isPage = (offset != null && count != null);

        LambdaQueryWrapper<ImChatBody> queryWrapper = new LambdaQueryWrapper<>();
        //
        String conversation = json.getConversation();
        if(StrUtil.isNotEmpty(conversation)){
            queryWrapper.eq(ImChatBody::getConversation,conversation);
        }
        //
        String qGroupId = json.getGroupId();
        if(StrUtil.isNotEmpty(qGroupId)){
            queryWrapper.eq(ImChatBody::getGroupId,qGroupId);
        }
        //
        String userId = json.getUserId();
        if(StrUtil.isNotEmpty(userId)){
            queryWrapper.eq(ImChatBody::getToId,userId);
        }
        //
        String fromUserId = json.getFromUserId();
        if(StrUtil.isNotEmpty(fromUserId)){
            queryWrapper.eq(ImChatBody::getFromId,fromUserId);
        }
        //
        if(isTimeBetween){
            queryWrapper.between(ImChatBody::getServiceCreateTime,beginTime,endTime);
        }
        /**
         * EQ 就是 EQUAL等于
         * NE就是 NOT EQUAL不等于
         * GT 就是 GREATER THAN大于　
         * LT 就是 LESS THAN小于
         * GE 就是 GREATER THAN OR EQUAL 大于等于
         * LE 就是 LESS THAN OR EQUAL 小于等于
         */
        Date maxTime = json.getMaxTime();
        if(maxTime != null){
            // 用小于，不用小于等于。避免数据重复。
            queryWrapper.lt(ImChatBody::getServiceCreateTime,maxTime);
        }

        // 排序
        queryWrapper.orderByDesc(ImChatBody::getServiceCreateTime);
        // 执行结果
        List<ImChatBody> imChatList = null;
        if(isPage){
            IPage<ImChatBody> page = new Page<>(offset, count);
//            IPage<ImChatBody> imChatBodyIPage = imChatBodyMapper.selectPage(page, queryWrapper);
//            imChatList = imChatBodyIPage.getRecords();
            imChatList = imChatBodyMapper.selectList(page,queryWrapper);
        }else{
            imChatList = imChatBodyMapper.selectList(queryWrapper);
        }
        // 排序，距离上面的时间是之前的内容。
        imChatList.sort((a,b)->{
            return (int) (a.getServiceCreateTime().getTime() -  b.getServiceCreateTime().getTime());
        });
        String curUserId = ImController.getCurUserId();
        // 转货扩展的内容。
        imChatList.forEach(x->{
            String extras0 = x.getExtras0();
            JSONObject extras = JSONObject.parseObject(extras0);

            //
            ImMsgExtras imMsgExtras = new ImMsgExtras();
            imMsgExtras.setId(x.getId());
            imMsgExtras.setUserId(curUserId);
            LambdaQueryWrapper<ImMsgExtras> imMsgExtrasWrapper = new LambdaQueryWrapper<>(imMsgExtras);
            imMsgExtras = imMsgExtrasMapper.selectOne(imMsgExtrasWrapper);
            if(imMsgExtras != null){
                extras.putAll(BeanUtil.beanToMap(imMsgExtras));
            }
            x.setExtras(extras);
        });
        return imChatList;
    }
}
